package com.anydata.android.weatherforecast.weather.util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.ListPreference;

import com.anydata.android.weatherforecast.weather.shell.WeatherPreference;

public class CityListPropertyUtil {

	public static void saveCityListForPreferences(ListPreference lp/*, WeatherPreference wp*/){
		CharSequence[] entryValues = lp.getEntryValues();
		int i=0;
		String entryValue = "";
		for(;i <entryValues.length; i++){
			entryValue += entryValues[i] + ",";
		}
		
		CharSequence[] entries = lp.getEntries();
		String entry = "";
		for(i=0;i <entries.length; i++){
			entry += entries[i] + ",";
		}
		SharedPreferences sp = WeatherPreference.wp.getPreferences(0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(WeatherPreference.CITY_ITEM, entry);
		editor.putString(WeatherPreference.CITY_LIST_NAME, entryValue);
		
		editor.commit();
	}
	
	public static void loadCityListForPreferences(ListPreference mCityListPref, WeatherPreference wp){
		int i=0;
		SharedPreferences sp = wp.getPreferences(Activity.MODE_PRIVATE);
		
		String city_item = sp.getString(WeatherPreference.CITY_ITEM, WeatherPreference.DEFAULT_CITY_ITEM_VALUE);
		String[] s1 = city_item.split(",");
		CharSequence[] c1 = new CharSequence[s1.length];
		for(i=0; i<c1.length; i++){
			c1[i] = s1[i];
		}
		mCityListPref.setEntries(c1);
		
		String city_list_name = sp.getString(WeatherPreference.CITY_LIST_NAME, WeatherPreference.DEFAULT_CITY_LIST_NAME_VALUE);
		String[] s2 = city_list_name.split(",");
		CharSequence[] c2 = new CharSequence[s2.length];
		for(i=0; i<c2.length; i++){
			c2[i] = s2[i];
		}
		mCityListPref.setEntryValues(c2);
	}
	
	public static void addCity(ListPreference mCityListPref, String newCity){
		
		newCity = makeCityNameFirstCaptial(newCity);
		CharSequence[] entry = mCityListPref.getEntries();
		CharSequence[] value = mCityListPref.getEntryValues();
		
		ArrayList entryList = new ArrayList();
		ArrayList valueList = new ArrayList();
		int i=0;
		for(; i<entry.length; i++){
			if(entry.toString().equalsIgnoreCase(newCity))
			{
				break;
			}
		}
		
		if(i == entry.length)
		{
			CharSequence[] entryNew = new CharSequence[entry.length + 1];
			CharSequence[] valueNew = new CharSequence[value.length + 1];
			for(int j=0; j<entry.length; j++){
				entryNew[j] = entry[j];
				valueNew[j] = value[j];
			}
			entryNew[entry.length] = newCity;
			valueNew[entry.length] = newCity;
			
			mCityListPref.setEntries(entryNew);
			mCityListPref.setEntryValues(valueNew);
			
			saveCityListForPreferences(mCityListPref);
		}
	}
	
	public static String makeCityNameFirstCaptial(String s){
		if(s == null)
			s = "";
		
		String[] list = s.split(" ");
		
		for(int i=0; i<list.length; i++){
			s = ("" + list[i].charAt(0)).toUpperCase();
			if(list[i].length() > 1){
				s += list[i].substring(1).toLowerCase();
			}
			
			s += " ";
		}
		return s;
	}
}
